package com.styletransfer.model;

import java.util.Collections;
import java.util.List;

/**
 * 分页响应类
 * 
 * 这个类用于封装分页查询的结果，包括当前页数据、总记录数、总页数等信息。
 * 前端可以根据这些信息展示分页数据和分页导航。
 * 
 * @param <T> 分页数据的类型
 */
public class PageResponse<T> {
    
    private List<T> list;        // 当前页数据列表
    private Long total;          // 总记录数
    private Integer pageNum;     // 当前页码
    private Integer pageSize;    // 每页记录数
    private Integer pages;       // 总页数
    private Boolean hasNext;     // 是否有下一页
    private Boolean hasPrev;     // 是否有上一页
    
    // 构造函数
    public PageResponse() {
    }
    
    public PageResponse(List<T> list, Long total, Integer pageNum, Integer pageSize) {
        this.list = list;
        this.total = total;
        this.pageNum = pageNum;
        this.pageSize = pageSize;
        this.pages = (int) Math.ceil((double) total / pageSize);
        this.hasNext = pageNum < pages;
        this.hasPrev = pageNum > 1;
    }
    
    // 静态方法，创建分页响应
    public static <T> PageResponse<T> of(List<T> list, Long total, Integer pageNum, Integer pageSize) {
        return new PageResponse<>(list, total, pageNum, pageSize);
    }
    
    public static <T> PageResponse<T> of(List<T> list, Long total, PageRequest pageRequest) {
        return new PageResponse<>(list, total, pageRequest.getPageNum(), pageRequest.getPageSize());
    }
    
    public static <T> PageResponse<T> empty() {
        return new PageResponse<>(Collections.emptyList(), 0L, 1, 10);
    }
    
    // Getters and Setters
    public List<T> getList() {
        return list;
    }
    
    public void setList(List<T> list) {
        this.list = list;
    }
    
    public Long getTotal() {
        return total;
    }
    
    public void setTotal(Long total) {
        this.total = total;
    }
    
    public Integer getPageNum() {
        return pageNum;
    }
    
    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }
    
    public Integer getPageSize() {
        return pageSize;
    }
    
    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
    
    public Integer getPages() {
        return pages;
    }
    
    public void setPages(Integer pages) {
        this.pages = pages;
    }
    
    public Boolean getHasNext() {
        return hasNext;
    }
    
    public void setHasNext(Boolean hasNext) {
        this.hasNext = hasNext;
    }
    
    public Boolean getHasPrev() {
        return hasPrev;
    }
    
    public void setHasPrev(Boolean hasPrev) {
        this.hasPrev = hasPrev;
    }
}
